#!groovy

// This "extended pipeline" runs longer tests:
// * builds a tarball as usual
// * uses that tarball for
//   + running Unix Debug checks
//   + building MinGW Windows zipballs
//   + running Embedded checks
//   + running Coverity and Sonarscanner analysis
//
// The GIT checkout must use the remote branch name as the checkout local
// branch name so that tarball names contain the branch name.
// In "Additional Behaviours", enable "Checkout to specific local branch"
// and leave "Branch name" empty.
// Not needed for multi-branch pipelines which already BRANCH_NAME in the environment.

def gitRepoURL = ""
def gitBranch = ""
def tarballgz
def tarballbz2
def statusHasChanged = false

pipeline {
        agent none

	// Trigger the build
	triggers {
		// Poll GitHub explicitly on per night, in case webhooks aren't used
		pollSCM('0 3 * * *')
	}

        stages {
		stage('Tarball') {
			steps {
				node('autotools') {
                                        checkout scm
					script {
						gitRepoURL = sh (script: 'git config --get remote.origin.url', returnStdout: true).trim()
						gitBranch = sh (script: 'if test "$BRANCH_NAME"; then echo $BRANCH_NAME; else git rev-parse --abbrev-ref HEAD; fi', returnStdout: true).trim()
					}
                                        sh 'contrib/ci.inria.fr/job-0-tarball.sh '+gitBranch
                                        script {
                                                tarballgz = sh (script: 'ls *.gz', returnStdout: true).trim()
                                                tarballbz2 = sh (script: 'ls *.bz2', returnStdout: true).trim()
                                        }
                                        stash includes: tarballgz, name: 'tarballgz'
                                        dir('contrib/ci.inria.fr') {
						stash includes: "job-3-debug.sh", name: 'script-unix-debug'
						stash includes: "job-3-embedded.sh", name: 'script-embedded'
						stash includes: "job-3-coverity.sh", name: 'script-coverity'
						stash includes: "job-3-sonarscanner.sh", name: 'script-sonarscanner'
						stash includes: "job-3-mingw.*", name: 'scripts-mingw'
						stash includes: "job-3-cygwin.*", name: 'scripts-cygwin'
					}
					archiveArtifacts artifacts: tarballgz+","+tarballbz2+",doc/doxygen-doc/hwloc-a4.pdf", fingerprint: true, onlyIfSuccessful: true
					deleteDir()
				}
			}
		}
		stage('Check') {
			steps {
				script {
					listOfNodeNames = []
					if (env.NO_UNIX != 'true') {
						listOfNodeNames = nodesByLabel('unix')
					}
					if (env.NO_MINGW != 'true') {
						listOfNodeNames.push('MinGW')
					}
					if (env.NO_COVERITY != 'true') {
						listOfNodeNames.push('Coverity')
					}
					if (env.NO_SONARQUBE != 'true') {
						listOfNodeNames.push('SonarQube Scanner')
					}
					if (env.NO_EMBEDDED != 'true') {
						listOfNodeNames.push('Embedded')
					}
					if (env.NO_CYGWIN != 'true') {
						listOfNodeNames.push('cygwin')
					}

					def p = listOfNodeNames.collectEntries {
					[ (it): {
						if (it == 'MinGW') {
							node('mingw') {
								dir('check-mingw') {
									unstash 'tarballgz'
									unstash 'scripts-mingw'
									bat 'job-3-mingw.bat '+tarballgz
									archiveArtifacts artifacts: "*.zip", fingerprint: true, onlyIfSuccessful: true
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else if (it == 'cygwin') {
							node('cygwin') {
								dir('check-cygwin') {
									unstash 'tarballgz'
									unstash 'scripts-cygwin'
									bat './job-3-cygwin.bat '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else if (it == 'Coverity') {
							node('coverity') {
								dir('check-coverity') {
									unstash 'tarballgz'
									unstash 'script-coverity'
									sh 'chmod 755 job-3-coverity.sh && ./job-3-coverity.sh '+gitRepoURL+' '+gitBranch+' '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else if (it == 'SonarQube Scanner') {
							node('sonarscanner') {
								dir('check-sonarscanner') {
									unstash 'tarballgz'
									unstash 'script-sonarscanner'
									sh 'chmod 755 job-3-sonarscanner.sh && ./job-3-sonarscanner.sh '+gitRepoURL+' '+gitBranch+' '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else if (it == 'Embedded') {
							node('autotools') {
								dir('check-embedded') {
									unstash 'tarballgz'
									unstash 'script-embedded'
									sh 'chmod 755 job-3-embedded.sh && ./job-3-embedded.sh '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else {
							node(it) {
								dir('check-unix-debug') {
									unstash 'tarballgz'
									unstash 'script-unix-debug'
									sh 'chmod 755 job-3-debug.sh && ./job-3-debug.sh '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						}
					}]}
					parallel p;
				}
			}
		}
	}

	post {
		// hooks are called in order: always, changed, aborted, failure, success, unstable
		changed {
			echo "Build status has changed."
			script {
				statusHasChanged = true
			}
		}
		success {
			echo "Build success."
			// email when changed to success
			script {
				if (statusHasChanged || env.EMAIL_NOTIFICATION == 'true') {
					emailext(body: '${DEFAULT_CONTENT}',
						 subject: '${DEFAULT_SUBJECT}',
						 replyTo: '$DEFAULT_REPLYTO',
						 to: '$DEFAULT_RECIPIENTS',
						 recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'RequesterRecipientProvider']])
				}
			}
		}
		failure {
			echo "Build failure."
			// always email on failure
			emailext(body: '${DEFAULT_CONTENT}',
				 subject: '${DEFAULT_SUBJECT}',
				 replyTo: '$DEFAULT_REPLYTO',
				 to: '$DEFAULT_RECIPIENTS',
				 recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'RequesterRecipientProvider']])
		}
	}
}
